package cn.tedu.springmvc.controller;

import cn.tedu.springmvc.co.UserCo;
import cn.tedu.springmvc.co.UserRegDTO;
import cn.tedu.springmvc.util.JasonResult;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import static cn.tedu.springmvc.util.JasonResult.State.ERR_PASSWORD;
import static cn.tedu.springmvc.util.JasonResult.State.ERR_USERNAME;

@RestController //必须加@Controller
@RequestMapping("/user")  //如果这里类上加上@RequestMapping("/user")  就要在地址栏上加 /user 才可以
public class UserController {
    public UserController(){
        System.out.println("UserController.UserController()");
    }

    @GetMapping("/npe.do")
    public String  npe(String  username){
        username.toString();
        return "ahahhaa";
    }

    @GetMapping("/cat.do")
    public Integer cat(Integer id){
        id.intValue();
        return 111;
    }







  @RequestMapping("/login.do")
    public JasonResult<Void> login(String username, String password){
        System.out.println("username = " + username + ", password = " + password);
      // 假设 admin / 888888 是正确的 用户名 / 密码
      // 判断用户名
      if ("admin".equals(username)){

          if ("88888".equals(password)){
              return JasonResult.ok();
          }else {
              String message = "登录失败，密码错误！";
              return JasonResult.fail(ERR_PASSWORD,message);
          }
      }else {
          String message = "登录失败，用户名不存在！";
          return JasonResult.fail(ERR_USERNAME,message);

      }

    }

    @RequestMapping("/reg.do")
    public JasonResult<Void> reg(UserRegDTO  userRegDTO){
        System.out.println(userRegDTO);

        return JasonResult.ok();
    }

    @GetMapping("/info.do")
    public UserCo info(){
        UserCo userCo = new UserCo();
        userCo.setUsername("xuebi");
        userCo.setPassword("111");
        return userCo;

    }
  /*  @GetMapping("/{id:[0-9]+}/info.do")
    public UserRegDTO info(@PathVariable String username){
        System.out.println("查询username="+username+"的用户信息");
        UserRegDTO  userRegDTO =new UserRegDTO();
        userRegDTO.setUsername("xuebi");
        userRegDTO.setPassword("1234");
        return userRegDTO;
    }
*/

    @GetMapping("/{id:[0-9]+}/info.do")
    public JasonResult<UserCo> info(@PathVariable Long id){
        System.out.println("查询id="+id+"的用户信息");
        UserCo  userCo =new UserCo();
        userCo.setUsername("xuebi");
        userCo.setPassword("1234");

        return JasonResult.ok(userCo);
    }


}
